package cc.rengu.igas.bomp.common.dao.impl;

import cc.rengu.igas.bomp.common.dao.OrgInfoMapper;
import cc.rengu.igas.bomp.common.entity.OrgInfo;
import cc.rengu.jradp.mods.xml.XmlTree;
import cc.rengu.oltp.service.common.constant.AppConfigConstant;
import cc.rengu.oltp.service.common.constant.AppParamConstant;
import cc.rengu.oltp.utility.bean.PageInfo;
import cc.rengu.oltp.utility.bean.PageResult;
import cc.rengu.oltp.utility.bean.RecursionInfo;
import cc.rengu.oltp.utility.util.DbsUtil;
import cc.rengu.oltp.utility.util.XmlConfigUtil;
import cc.rengu.oltp.utility.util.XmlTreeUtil;
import cc.rengu.utility.cache.UnifiedCache;
import cc.rengu.utility.log.RgLog;
import cc.rengu.utility.log.RgLogger;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 机构信息表操作实现类
 * Created by wangcheng on 2021/7/2 11:22
 */
public class OrgInfoMapperImpl implements OrgInfoMapper {
    private final RgLogger rglog = RgLog.getLogger(this.getClass().getName());
    private String dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(AppConfigConstant.DATABASE_CONF_NODE, AppConfigConstant.MCMP_POOL_NAME);

    public OrgInfoMapperImpl() throws Exception {
        if (XmlTree.hasThreadTree()) {
            XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
            String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
            if (null != corporation && !corporation.isEmpty()) {
                String dbPoolCashNode = corporation.trim() + AppConfigConstant.DATABASE_CONF_NODE;
                dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppConfigConstant.MCMP_POOL_NAME);
            }
        }
    }

    @Override
    public int insertOrgInfo(OrgInfo orgInfo) throws Exception {
        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "insertOrgInfo");
        int iReturnCode = dbsUtil.dbsExecuteSql(sqlStr, orgInfo, null);
        if (0 != iReturnCode) {
            rglog.error("登记机构信息失败,instId:<{}>,orgCode:<{}>,orgName:<{}>,iReturnCode:<{}>", orgInfo.getInstId(), orgInfo.getOrgCode(), orgInfo.getOrgName(), iReturnCode);
        }
        return iReturnCode;
    }

    @Override
    public int updateOrgInfoById(OrgInfo orgInfo) throws Exception {
        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "updateOrgInfoById");
        int iReturnCode = dbsUtil.dbsExecuteSql(sqlStr, orgInfo, null);
        if (0 != iReturnCode) {
            rglog.error("根据非业务主键更新机构信息失败,instId:<{}>,orgCode:<{}>,orgName:<{}>,iReturnCode:<{}>", orgInfo.getInstId(), orgInfo.getOrgCode(), orgInfo.getOrgName(), iReturnCode);
        }
        return iReturnCode;
    }

    @Override
    public int updateOrgInfoByPrimaryKey(OrgInfo orgInfo) throws Exception {
        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "updateOrgInfoByPrimaryKey");
        int iReturnCode = dbsUtil.dbsExecuteSql(sqlStr, orgInfo, null);
        if (0 != iReturnCode) {
            rglog.error("根据业务主键更新机构信息失败,instId:<{}>,orgCode:<{}>,orgName:<{}>,iReturnCode:<{}>", orgInfo.getInstId(), orgInfo.getOrgCode(), orgInfo.getOrgName(), iReturnCode);
        }
        return iReturnCode;
    }

    @Override
    public OrgInfo selectOrgInfoByPrimaryKey(String instId, String orgType, String orgCode) throws Exception {
        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "selectOrgInfoByPrimaryKey");
        OrgInfo orgInfo = new OrgInfo();
        orgInfo.setInstId(instId);
        orgInfo.setOrgType(orgType);
        orgInfo.setOrgCode(orgCode);
        int iReturnCode = dbsUtil.dbsExecuteSql(sqlStr, orgInfo, orgInfo);
        if (0 != iReturnCode) {
            rglog.error("根据业务主键查询机构信息失败,instId:<{}>,orgCode:<{}>,iReturnCode:<{}>", instId, orgCode, iReturnCode);
            return null;
        }
        return orgInfo;
    }

    @Override
    public List<OrgInfo> selectOrgInfoByOrgCodeOrOrgName(String instId, String orgType, String orgCode, String orgName) throws Exception {
        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "selectOrgInfoByOrgCodeOrOrgName");
        OrgInfo orgInfo = new OrgInfo();
        orgInfo.setInstId(instId);
        orgInfo.setOrgType(orgType);
        orgInfo.setOrgCode(orgCode);
        orgInfo.setOrgName(orgName);
        List<Object> objectList = dbsUtil.dbsFetchLimitListData(sqlStr, orgInfo, OrgInfo.class);
        if (null == objectList || objectList.isEmpty()) {
            rglog.warn("根据机构号或者机构名称模糊查询机构信息失败,instId:<{}>,orgCode:<{}>,orgName:<{}>", instId, orgCode, orgName);
            return null;
        }
        return objectList.stream().map(item -> (OrgInfo) item).collect(Collectors.toList());
    }

    @Override
    public PageResult selectOrgInfoByOrgCodeOrOrgName(String instId, String orgType, String orgCode, String orgName, PageInfo pageInfo) throws Exception {
        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "selectOrgInfoByOrgCodeOrOrgName");
        OrgInfo orgInfo = new OrgInfo();
        orgInfo.setInstId(instId);
        orgInfo.setOrgType(orgType);
        orgInfo.setOrgCode(orgCode);
        orgInfo.setOrgName(orgName);
        PageResult pageResult = dbsUtil.dbsFetchPageListData(sqlStr, orgInfo, OrgInfo.class, pageInfo);
        if (null == pageResult || pageResult.getResult().isEmpty()) {
            rglog.error("根据机构号或者机构名称模糊分页查询机构信息失败,instId:<{}>,orgCode:<{}>,orgName:<{}>", instId, orgCode, orgName);
            return null;
        }
        return pageResult;
    }

    @Override
    public List<OrgInfo> recursionQueryByOrgCode(String instId, String orgType, String orgCode) throws Exception {
        List<OrgInfo> orgInfoList = new ArrayList<>();
        /* 先查询当前机构信息 */
        OrgInfo orgInfo = selectOrgInfoByPrimaryKey(instId, orgType, orgCode);
        if (null == orgInfo) {
            return null;
        }
        orgInfoList.add(orgInfo);
        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "recursionQueryByOrgCode");
        OrgInfo superOrgInfo = new OrgInfo();
        superOrgInfo.setInstId(instId);
        orgInfo.setOrgType(orgType);
        superOrgInfo.setSuperOrgCode(orgCode);
        List<Object> objectList = dbsUtil.dbsRecursionQuery(sqlStr, superOrgInfo, OrgInfo.class, "superOrgCode", "orgCode");
        if (null == objectList || objectList.isEmpty()) {
            rglog.warn("根据当前机构递归查询其下属机构信息失败,instId:<{}>,orgCode:<{}>", instId, orgCode);
            return orgInfoList;
        }
        orgInfoList.addAll(objectList.stream().map(item -> (OrgInfo) item).collect(Collectors.toList()));
        return orgInfoList;
    }

    @Override
    public PageResult recursionPageQueryByOrgCode(String instId, String orgType, String orgCode, PageInfo pageInfo) throws Exception {
        List<String> recursionValueList = new ArrayList<>();
        List<OrgInfo> orgInfos = recursionQueryByOrgCode(instId, orgType, orgCode);
        if (null != orgInfos && !orgInfos.isEmpty()) {
            orgInfos.forEach(item -> recursionValueList.add(item.getOrgCode()));
        }
        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "recursionPageQueryByOrgCode");
        OrgInfo orgInfo = new OrgInfo();
        orgInfo.setInstId(instId);
        orgInfo.setOrgType(orgType);
        orgInfo.setSuperOrgCode(orgCode);
        RecursionInfo recursionInfo = new RecursionInfo();
        recursionInfo.setConditionField("orgCode");
        recursionInfo.setRecursionValueList(recursionValueList);
        PageResult pageResult = dbsUtil.dbsRecursionPageQuery(sqlStr, orgInfo, OrgInfo.class, pageInfo, recursionInfo);
        if (null == pageResult || pageResult.getResult().isEmpty()) {
            rglog.error("根据业务主键递归分页查询机构信息失败,instId:<{}>,orgCode:<{}>", instId, orgCode);
            return null;
        }
        return pageResult;
    }
}
